package com.jason.item.controller;

import com.jason.common.base.BaseController;
import com.jason.common.error.BusinessErrorEnum;
import com.jason.common.error.BusinessException;
import com.jason.common.vo.CommonReturnType;
import com.jason.item.controller.vo.ItemVO;
import com.jason.item.model.ItemModel;
import com.jason.item.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/item")
@Api(value = "api接口", description = "商品相关操作")
public class ItemController extends BaseController {

    @Autowired
    private ItemService itemService;

    @ApiOperation(value = "新增商品", notes = "新增商品信息")
    @ApiImplicitParam(name = "itemModel", value = "商品模型", required = true, paramType = "query", dataType = "ItemModel")
    @PostMapping("/creator")
    public CommonReturnType createItem(@Valid ItemModel itemModel) {

        ItemModel item = itemService.createItem(itemModel);
        ItemVO itemVO = ItemVO.convertByItemModel(item);
        return CommonReturnType.success(itemVO);
    }

    @ApiOperation(value = "修改商品", notes = "根据商品ID修改商品信息")
    @ApiImplicitParam(name = "itemModel", value = "商品模型", required = true, paramType = "query", dataType = "ItemModel")
    @PostMapping("/modifier")
    public CommonReturnType updateItem(ItemModel itemModel) throws BusinessException {
        if (itemModel.getId() == null) {
            throw new BusinessException(BusinessErrorEnum.PARAM_ILLEGAL_ERROR, "商品ID不能为空");
        }
        ItemModel item = itemService.updateItem(itemModel);
        ItemVO itemVO = ItemVO.convertByItemModel(item);
        return CommonReturnType.success(itemVO);
    }

    @ApiOperation(value = "查询单个商品", notes = "根据商品ID查询单个商品")
    @ApiImplicitParam(name = "id", value = "商品ID", required = true,  paramType = "query", dataType = "Integer")
    @GetMapping("id/{id}")
    public CommonReturnType getItemDetail(@PathVariable("id") Integer id) {

        ItemModel item = itemService.getItemById(id);
        ItemVO itemVO = ItemVO.convertByItemModel(item);
        return CommonReturnType.success(itemVO);
    }


    @GetMapping("/list")
    @ApiOperation(value = "查询商品列表", notes = "根据条件查询商品列表")
    // @ApiImplicitParam(name = "id", value = "商品ID", required = true,  paramType = "query", dataType = "Integer")
    public CommonReturnType listItem(@RequestParam(name = "t", required = false) String t) {
        log.info("查询时间:{}",t);

        List<ItemModel> itemModelList = itemService.listItem();
        List<ItemVO> itemVOList = itemModelList.stream().map(itemModel -> {
            ItemVO itemVO = ItemVO.convertByItemModel(itemModel);
            return itemVO;
        }).collect(Collectors.toList());
        log.info("listItem:{}", itemVOList);
        return CommonReturnType.success(itemVOList);
    }
    @DeleteMapping("/id/{id}")
    public CommonReturnType delItem(@PathVariable(name = "id") Integer id) {
        itemService.removeItemById(id);
        return CommonReturnType.success("删除成功");
    }
}
